{% extends 'base.html' %}
{% load paper_filters %}

{% block title %}Enhanced Search Results - Research Paper Analyzer{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <!-- Header with Export Options -->
        <div class="card mb-4">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h3 class="mb-0">
                            <i class="fas fa-search me-2"></i>Enhanced Search Results
                        </h3>
                        <p class="text-muted mb-0">
                            Query: <strong>"{{ query }}"</strong> | 
                            Papers found: <strong>{{ papers_count }}</strong>
                        </p>
                    </div>
                    <div class="btn-group">
                        <a href="{% url 'papers:export_csv' %}" class="btn btn-outline-success">
                            <i class="fas fa-file-csv me-1"></i>Export CSV
                        </a>
                        <a href="{% url 'papers:export_json' %}" class="btn btn-outline-info">
                            <i class="fas fa-file-code me-1"></i>Export JSON
                        </a>
                        <a href="{% url 'papers:search' %}" class="btn btn-outline-primary">
                            <i class="fas fa-search me-1"></i>New Search
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Comprehensive Synthesis -->
        {% if comprehensive_synthesis %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-brain me-2"></i>Comprehensive Synthesis
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-8">
                        <div class="synthesis-content">
                            {{ comprehensive_synthesis|linebreaks }}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="text-center">
                            <div class="display-4 text-primary">{{ papers_count }}</div>
                            <p class="text-muted">Papers Analyzed</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Analysis Summary -->
        {% if analysis_summary %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-chart-bar me-2"></i>Analysis Summary
                </h5>
            </div>
            <div class="card-body">
                <p class="lead">{{ analysis_summary|truncatewords:100 }}</p>
            </div>
        </div>
        {% endif %}

        <!-- Impact Assessment -->
        {% if impact_assessment %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-chart-line me-2"></i>Impact Assessment
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% if impact_assessment.theoretical_impact %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-lightbulb text-warning me-1"></i>Theoretical Impact</h6>
                        <p class="small">{{ impact_assessment.theoretical_impact }}</p>
                    </div>
                    {% endif %}
                    {% if impact_assessment.practical_applications %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-cogs text-info me-1"></i>Practical Applications</h6>
                        <p class="small">{{ impact_assessment.practical_applications }}</p>
                    </div>
                    {% endif %}
                    {% if impact_assessment.policy_implications %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-balance-scale text-success me-1"></i>Policy Implications</h6>
                        <p class="small">{{ impact_assessment.policy_implications }}</p>
                    </div>
                    {% endif %}
                    {% if impact_assessment.industry_relevance %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-industry text-primary me-1"></i>Industry Relevance</h6>
                        <p class="small">{{ impact_assessment.industry_relevance }}</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Methodology Comparison -->
        {% if methodology_comparison %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-microscope me-2"></i>Methodology Comparison
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% if methodology_comparison.common_approaches %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-check-circle text-success me-1"></i>Common Approaches</h6>
                        <ul class="list-unstyled">
                            {% for approach in methodology_comparison.common_approaches %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ approach }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                    {% if methodology_comparison.innovations %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-star text-warning me-1"></i>Methodological Innovations</h6>
                        <ul class="list-unstyled">
                            {% for innovation in methodology_comparison.innovations %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ innovation }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                    {% if methodology_comparison.gaps %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-exclamation-triangle text-danger me-1"></i>Methodological Gaps</h6>
                        <ul class="list-unstyled">
                            {% for gap in methodology_comparison.gaps %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ gap }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Statistical Data -->
        {% if statistical_data %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-chart-pie me-2"></i>Statistical Data
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% for stat in statistical_data %}
                    <div class="col-md-6 mb-3">
                        <div class="card border">
                            <div class="card-body">
                                <h6 class="card-title">{{ stat.paper_title|truncatechars:50 }}</h6>
                                <div class="row">
                                    {% if stat.sample_size %}
                                    <div class="col-6">
                                        <small class="text-muted">Sample Size</small>
                                        <p class="mb-0">{{ stat.sample_size }}</p>
                                    </div>
                                    {% endif %}
                                    {% if stat.statistical_tests %}
                                    <div class="col-6">
                                        <small class="text-muted">Tests Used</small>
                                        <p class="mb-0">{{ stat.statistical_tests }}</p>
                                    </div>
                                    {% endif %}
                                    {% if stat.significance %}
                                    <div class="col-6">
                                        <small class="text-muted">Significance</small>
                                        <p class="mb-0">{{ stat.significance }}</p>
                                    </div>
                                    {% endif %}
                                    {% if stat.effect_size %}
                                    <div class="col-6">
                                        <small class="text-muted">Effect Size</small>
                                        <p class="mb-0">{{ stat.effect_size }}</p>
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Citation Analysis -->
        {% if citations %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-link me-2"></i>Citation Analysis
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% if citations.common_references %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-book text-primary me-1"></i>Common References</h6>
                        <ul class="list-unstyled">
                            {% for ref in citations.common_references %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ ref }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                    {% if citations.influential_papers %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-star text-warning me-1"></i>Influential Papers</h6>
                        <ul class="list-unstyled">
                            {% for paper in citations.influential_papers %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ paper }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Cross-References -->
        {% if cross_references %}
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-project-diagram me-2"></i>Cross-References
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    {% if cross_references.builds_on %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-arrow-up text-success me-1"></i>Builds On</h6>
                        <ul class="list-unstyled">
                            {% for ref in cross_references.builds_on %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ ref }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                    {% if cross_references.conflicts %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-exclamation-triangle text-danger me-1"></i>Conflicting Findings</h6>
                        <ul class="list-unstyled">
                            {% for conflict in cross_references.conflicts %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ conflict }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                    {% if cross_references.complementary %}
                    <div class="col-md-6 mb-3">
                        <h6><i class="fas fa-handshake text-info me-1"></i>Complementary Research</h6>
                        <ul class="list-unstyled">
                            {% for comp in cross_references.complementary %}
                            <li><i class="fas fa-arrow-right text-muted me-1"></i>{{ comp }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Papers List -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-file-alt me-2"></i>Analyzed Papers
                </h5>
            </div>
            <div class="card-body">
                {% if papers %}
                    <div class="row">
                        {% for paper in papers %}
                            <div class="col-lg-6 mb-4">
                                <div class="card h-100 border">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0 text-primary">{{ paper.title|truncatechars:80 }}</h6>
                                    </div>
                                    <div class="card-body">
                                        <div class="row mb-2">
                                            <div class="col-6">
                                                <small class="text-muted">
                                                    <i class="fas fa-users me-1"></i>
                                                    {{ paper.authors|truncatechars:40 }}
                                                </small>
                                            </div>
                                            <div class="col-6 text-end">
                                                <small class="text-muted">
                                                    <i class="fas fa-calendar me-1"></i>
                                                    {{ paper.year|default:"N/A" }}
                                                </small>
                                            </div>
                                        </div>
                                        
                                        <p class="card-text small">
                                            {{ paper.abstract|truncatewords:30 }}
                                        </p>
                                        
                                        <!-- Enhanced Extracted Information -->
                                        {% if paper.main_findings %}
                                        <div class="mb-2">
                                            <strong class="small text-success">Main Findings:</strong>
                                            <p class="small mb-1">{{ paper.main_findings|truncatewords:20 }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.methodology %}
                                        <div class="mb-2">
                                            <strong class="small text-info">Methodology:</strong>
                                            <p class="small mb-1">{{ paper.methodology|truncatewords:15 }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.interventions %}
                                        <div class="mb-2">
                                            <strong class="small text-warning">Interventions:</strong>
                                            <p class="small mb-1">{{ paper.interventions|truncatewords:15 }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.outcomes %}
                                        <div class="mb-2">
                                            <strong class="small text-success">Outcomes:</strong>
                                            <p class="small mb-1">{{ paper.outcomes|truncatewords:15 }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.statistical_data %}
                                        <div class="mb-2">
                                            <strong class="small text-primary">Statistical Data:</strong>
                                            <p class="small mb-1">{{ paper.statistical_data|truncatewords:15 }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        {% if paper.limitations %}
                                        <div class="mb-2">
                                            <strong class="small text-danger">Limitations:</strong>
                                            <p class="small mb-1">{{ paper.limitations|truncatewords:15 }}</p>
                                        </div>
                                        {% endif %}
                                        
                                        <div class="mt-auto">
                                            <span class="badge bg-secondary">{{ paper.source|default:"Unknown" }}</span>
                                            {% if paper.doi %}
                                            <span class="badge bg-info">{{ paper.doi }}</span>
                                            {% endif %}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-search fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">No papers found</h5>
                        <p class="text-muted">Try adjusting your search query or parameters.</p>
                        <a href="{% url 'papers:search' %}" class="btn btn-primary">
                            <i class="fas fa-search me-1"></i>Try New Search
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>

        <!-- Export Section -->
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-download me-2"></i>Export Results
                </h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <h6>Enhanced CSV Export</h6>
                        <p class="small text-muted">Download all detailed analysis data in spreadsheet format.</p>
                        <a href="{% url 'papers:export_csv' %}" class="btn btn-success">
                            <i class="fas fa-file-csv me-1"></i>Download CSV
                        </a>
                    </div>
                    <div class="col-md-6">
                        <h6>JSON Export</h6>
                        <p class="small text-muted">Download complete enhanced analysis data in JSON format.</p>
                        <a href="{% url 'papers:export_json' %}" class="btn btn-info">
                            <i class="fas fa-file-code me-1"></i>Download JSON
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // Add click handlers for paper cards
    $('.card').hover(
        function() {
            $(this).addClass('shadow-sm');
        },
        function() {
            $(this).removeClass('shadow-sm');
        }
    );
    
    // Initialize tooltips
    $('[data-bs-toggle="tooltip"]').tooltip();
    
    // Add collapsible sections
    $('.card-header').click(function() {
        $(this).next('.card-body').slideToggle();
    });
});
</script>
{% endblock %} 